#!/usr/bin/env python


# roc.py: given two files (matchscores.txt and nonmatchscores.txt)
# compute the data on the ROC curve.
# The output is a CSV file.  The inputs are CSV files also.
#
# 2/13/08 PJF
# 3/17/08 PJF reworked for M+NM
# 3/26/08 PJF sampled  instead of enumerating all op points.
#

import sys

lowerbetter = False

# read matching matrix entries
fmatch = file('match.txt','r')

if not fmatch:
	print 'match file open failed.'
	sys.exit(-1)

# gobble up all of the lines in the file.
matchlines = fmatch.readlines()
fmatch.close()  

matchscores = []
for l in matchlines:
	matchscores.append(float(l.strip()))


fnonmatch = file('nomatch.txt','r')
if not fnonmatch:
	print 'nonmatch file open failed.'
	sys.exit(-1)
nonmatchlines = fnonmatch.readlines()
fnonmatch.close()

nonmatchscores = []
for l in nonmatchlines:
	nonmatchscores.append(float(l.strip()))


print len(matchscores),' match scores extracted.'
print len(nonmatchscores),' nonmatch scores extracted.'

matchscores.sort()
nonmatchscores.sort()

#
# OK, here's the hard part. The decision of a match versus nonmatch is based on a threshold on
# matching score.  When a score is computed, we decide whether it is a match or not.
# Since the truth isn't known, we could have four cases: correct match, false match, correct nonmatch, false nonmatch.
#

scores = matchscores + nonmatchscores
scores.sort()

sys.stdout.write('threshold,far,frr\n')

skip = 10
print >>sys.stderr,'Using skip of ',skip


if lowerbetter:
	while scores:
		thr = scores.pop()
		scores = scores[:-(skip-1)]
		nfa = 0
		nfr = 0
		for x in matchscores: # remember, lower is better
			if x > thr:
				nfr = nfr+1
		for x in nonmatchscores:
			if x <= thr:
				nfa = nfa+1
		far = 1.0*nfa/len(nonmatchscores)
		frr = 1.0*nfr/len(matchscores)
		sys.stdout.write('%g,%g,%g\n' % (thr,far,frr))
else:
	while scores:
		thr = scores.pop()
		scores = scores[:-(skip-1)]
		nfa = 0
		nfr = 0
		for x in matchscores: # remember, higher is better
			if x < thr:
				nfr = nfr+1
		for x in nonmatchscores:
			if x >= thr:
				nfa = nfa+1
		far = 1.0*nfa/len(nonmatchscores)
		frr = 1.0*nfr/len(matchscores)
		sys.stdout.write('%g,%g,%g\n' % (thr,far,frr))
